package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.*;
import com.gk.carshopsys.service.AfterOrderService;
import com.gk.carshopsys.service.CarOrderService;
import com.gk.carshopsys.utils.AfterOrderDataListener;
import com.gk.carshopsys.utils.CarOrderDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AfterOrderController {
    @Autowired
    AfterOrderService afterOrderService;


    //Excel参数
    private HashMap<String,String> param;


    //excel传参
    @PostMapping("/afterorderlist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }

    //excel下载
    @Log("导出售后订单")
    @GetMapping("/afterorderlist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
        ExcelUtils.export2Web(response, "售后订单表", "订单信息", AfterOrder.class,afterOrderService.findAfterOrder(param));
        return "success";
    }

    // easyexcel上传文件
    @Log("导入售后订单")
    @PostMapping("/afterorderlist/excelimport")
    public String upload(MultipartFile file, RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), AfterOrderImport.class, new AfterOrderDataListener(afterOrderService)).sheet().doRead();
        return "redirect:/afterorderlist";
    }


    @RequestMapping("/afterorderlist")
    public String toCarOrder(){
        return "afterorderlist";
    }

    @Log("查询售后订单")
    @RequestMapping("/afterorderlist/query")
    @ResponseBody
    public List<CarOrder> quryCarOrderList(@RequestBody HashMap<String,String> params){
        return afterOrderService.findAfterOrder(params);
    }


    @Log("添加售后订单")
    @RequestMapping("/afterorderlist/add")
    public String addCarOrder(@RequestParam("add_carorder") String carorder,
                              @RequestParam("add_carno") String carno,
                              @RequestParam("add_empnum") String empnum,
                              @RequestParam("add_service") String service,
                              @RequestParam("add_price") Integer price,
                              @RequestParam("add_state") String state,
                              @RequestParam("add_remarks") String remarks, RedirectAttributesModelMap model){

        int result = afterOrderService.addAfterOrder(carorder,carno,empnum,service,price,state,remarks);


        if (result==-1){
            model.addFlashAttribute("addmsg","添加失败，关联订单不存在");
            return "redirect:/afterorderlist";
        }
        if (result==-2){
            model.addFlashAttribute("addmsg","添加失败，订单未完成");
            return "redirect:/afterorderlist";
        }
        if (result==-3){
            model.addFlashAttribute("addmsg","添加失败，员工不存在");
            return "redirect:/afterorderlist";
        }
        if (result==1){
            model.addFlashAttribute("addmsg","添加订单成功");
            return "redirect:/afterorderlist";
        }else{
            model.addFlashAttribute("addmsg","添加订单失败，未知错误");
            return "redirect:/afterorderlist";
        }



    }


    //批量删除订单
    //订单批量删除
    @Log("删除售后订单")
    @ResponseBody
    @RequestMapping("/afterorderlist/del")
    public Map<String,Integer> carorderlistdel(@RequestBody List<Integer> ids){
        int result = afterOrderService.delAfterOrder(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }

    @Log("更新售后订单")
    @RequestMapping("/afterorderlist/update")
    public String updateCarOrder(@RequestParam("update_id") Long id,
                                 @RequestParam("update_state") String state,
                                 @RequestParam("update_remarks") String remarks, RedirectAttributesModelMap model){

        AfterOrder afterOrder = new AfterOrder();
        afterOrder.setId(id);
        afterOrder.setAfterremarks(remarks);
        if(state.equals("取消订单")){
            afterOrder.setState("已取消");
        }else{
            afterOrder.setState(state);
        }
        int result = afterOrderService.updateAfterOrderById(afterOrder);

        if (result==1){
            model.addFlashAttribute("addmsg","修改订单成功");
            return "redirect:/afterorderlist";
        }else{
            model.addFlashAttribute("addmsg","修改订单失败，未知错误");
            return "redirect:/afterorderlist";
        }

    }






}
